package com.iwhalecloud.bss.kite.cucc.web.service.accept.handler.attr;

import com.iwhalecloud.bss.kite.client.api.scene.operate.IOperateSceneCommonService;
import com.iwhalecloud.bss.kite.client.dto.DataResult;
import com.iwhalecloud.bss.kite.client.vo.scene.CommonAttrVO;
import com.iwhalecloud.bss.kite.common.constant.ServiceOfferConsts;
import com.iwhalecloud.bss.kite.common.util.KiteBeanUtils;
import com.iwhalecloud.bss.kite.common.util.KiteStringUtils;
import com.iwhalecloud.bss.kite.cucc.client.api.inf.IInfServiceBus;
import com.iwhalecloud.bss.kite.cucc.common.constant.CuccServiceOfferConsts;
import com.iwhalecloud.bss.kite.cucc.common.constant.LocalKeyConsts;
import com.iwhalecloud.bss.kite.cucc.service.util.ProdCateUtil;
import com.iwhalecloud.bss.kite.dataservice.cache.DcPublicCache;
import com.iwhalecloud.bss.kite.dataservice.entity.DcPublic;
import com.iwhalecloud.bss.kite.manager.api.IQuerySceneInstService;
import com.iwhalecloud.bss.kite.manager.inst.KiteAttr;
import com.iwhalecloud.bss.kite.manager.inst.KiteInst;
import com.iwhalecloud.bss.kite.manager.inst.KiteSceneInst;
import com.iwhalecloud.bss.kite.web.context.KiteWebContext;
import com.iwhalecloud.bss.kite.web.service.accept.attr.handler.AbstractAttrHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * @author chen.cheng110
 * @version V1.0
 * @Description: 五大双线商机查询服务
 * @date 2021年7月2日
 * @Package com.iwhalecloud.bss.kite.cucc.web.service.accept.handler.attr
 */
@Service
public class BusinessOppoDoubleLineAttrHandler extends AbstractAttrHandler {

    @Autowired
    private IQuerySceneInstService querySceneInstService;

    @Autowired
    IInfServiceBus infServiceBus;

    @Autowired
    private IOperateSceneCommonService operateSceneCommonService;

    @Override
    public void init(CommonAttrVO commonAttr) {
        KiteAttr attr = KiteBeanUtils.transform(commonAttr, KiteAttr.class);
        attr.setModuleId(LocalKeyConsts.GROUPUSERINFO_MODULEID);
        // 五大双线，订购的时候不需要展示商机，资源核查展示商机，后续环节商机不允许修改
        String sceneInstId = KiteWebContext.getContextNew().getCurrentSceneInstId();
        KiteSceneInst sceneInst = querySceneInstService.getSceneInst(sceneInstId);
        String serviceOfferId = sceneInst.getServiceOfferId();
        String cateId = querySceneInstService.getOfferInstAttrValue(KiteWebContext.getContext().getCustId(), sceneInst.getRootInstId(), "cateId");
        String bizType = querySceneInstService.getExtParameterValue(sceneInstId, "bizType");
        String nodeCode = querySceneInstService.getExtParameterValue(sceneInstId, "nodeCode");
        if (KiteStringUtils.isEmpty(nodeCode)) {
            nodeCode = "OrderInput";
        }
        if (KiteStringUtils.equalsAny(cateId, ProdCateUtil.getDoubleLineCateIdForArr())) {

            KiteInst kiteInst = querySceneInstService.getKiteInst(sceneInst.getCustId(), sceneInst.getRootInstType(), sceneInst.getRootInstId());
            List<DcPublic> list = DcPublicCache.get("20210719001");
            // 是否五大双线落地方产品，不显示商机
            boolean flag = (list != null) && list.stream().anyMatch(dcPublic -> KiteStringUtils.isEqual(kiteInst.getSpecId(), dcPublic.getCodeb()));

            // 国际互联网，不显示商机
            boolean isOutNet = KiteStringUtils.isEqual(kiteInst.getSpecId(), "50040853");

            String renderOrderType = querySceneInstService.getExtParameterValue(sceneInstId, "renderOrderType");

            // 订购信息录入
            if (flag || isOutNet || (KiteStringUtils.equals(serviceOfferId, ServiceOfferConsts.SERVICE_OFFER_ORDER) && KiteStringUtils.isEmpty(bizType) && KiteStringUtils.equals(nodeCode, "OrderInput"))) {
                commonAttr.setShowType("E");
                commonAttr.setIsEdit("F");
                attr.setShowType("E");
                attr.setIsEdit("F");
            }
            // 资源核查信息录入、资源核查甩单补录
            else if (KiteStringUtils.isEqual(renderOrderType, "orderRejection") ||
                (KiteStringUtils.equals(serviceOfferId, CuccServiceOfferConsts.SERVICE_OFFER_RESOURCE_CHECK) && KiteStringUtils.isEmpty(bizType) && KiteStringUtils.equals(nodeCode, "OrderInput"))) {
                commonAttr.setShowType("D");
                commonAttr.setIsEdit("T");
                attr.setShowType("D");
                attr.setIsEdit("T");
            } else {
                commonAttr.setShowType("D");
                commonAttr.setIsEdit("F");
                attr.setShowType("D");
                attr.setIsEdit("F");
            }
        }
        operateSceneCommonService.setCommonAttr(sceneInstId, attr);
    }

    @Override
    public DataResult<Boolean> apply(String sceneInstId, String fieldName, String value, String valueDesc) {
        if (KiteStringUtils.isEqual(LocalKeyConsts.BUSINESS_NAME, fieldName) || KiteStringUtils.isEqual(LocalKeyConsts.BUSINESS_NAME_NEW, fieldName)) {
            KiteAttr attr = querySceneInstService.getCommonAttrsByFieldName(sceneInstId, LocalKeyConsts.BUSINESS, LocalKeyConsts.GROUPUSERINFO_MODULEID);
            KiteAttr oppoAttr = querySceneInstService.getCommonAttr(sceneInstId, fieldName);
            if (Objects.isNull(attr)) {
                attr = querySceneInstService.getCommonAttrsByFieldName(sceneInstId, LocalKeyConsts.BUSINESS_NEW, LocalKeyConsts.GROUPUSERINFO_MODULEID);
            }
            if (Objects.nonNull(attr) && Objects.nonNull(oppoAttr)) {
                // 回填的时候，商机名称value和valueDesc是一样的，当在录入的时候选择商机名称触发联动时，value是商机编码
                if (!KiteStringUtils.equals(value, valueDesc)) {
                    attr.setValue(value);
                    attr.setValueDesc(valueDesc);
                    operateSceneCommonService.setCommonAttr(sceneInstId, attr);

                    oppoAttr.setValue(valueDesc);
                    oppoAttr.setValueDesc(valueDesc);
                    operateSceneCommonService.setCommonAttr(sceneInstId, oppoAttr);
                }
            }
        }
        return super.apply(sceneInstId, fieldName, valueDesc, valueDesc);
    }
}
